package com.bawei.splash

import android.os.CountDownTimer
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.base.RoutePath
import com.bawei.common.adapter.ImageBannerAdapter
import com.bawei.common.base.BaseActivity
import com.bawei.common.entity.BannerEntity
import com.bawei.router.degrade.LocalDegradeService
import com.bawei.splash.databinding.ActivitySplashBinding
import com.tencent.mmkv.MMKV
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.listener.OnPageChangeListener
import com.youth.banner.transformer.AlphaPageTransformer


/**
 * 启动页业务逻辑
 * 通用功能：APP主图展示（轮播效果），第三方广告（开屏广告），或公司自己研发广告（图片，视频）
 * 业务逻辑：第一次安装进入展示APP主图或公司相关介绍，如果是第二次进入会展示广告功能
 */
class SplashActivity : BaseActivity<ActivitySplashBinding>() {

    private lateinit var mCountDownTimer: CountDownTimer
    private var bannerData = mutableListOf<BannerEntity>()
    private lateinit var mBannerAdapter: ImageBannerAdapter


    override fun getLayoutId(): Int = R.layout.activity_splash

    override fun initView() {
        //判断是否为第一次打开
        var isFirstBoot = MMKV.defaultMMKV().decodeBool("isFirstBoot", true)
        //展示应用主图
        if (isFirstBoot){
            MMKV.defaultMMKV().encode("isFirstBoot", false)
            showAppBannerLayout()
        }
        //展示广告
        else{
            showAppAdvLayout()
        }
    }


    private fun showAppBannerLayout(){
        mDataBinding.clAppBanner.visibility = View.VISIBLE
        mDataBinding.clAppAdv.visibility = View.INVISIBLE

        //设置Banner数据
        bannerData.run {
            add(BannerEntity(0, R.drawable.wellcome_0, ""))
            add(BannerEntity(1, R.drawable.wellcome_1, ""))
            add(BannerEntity(2, R.drawable.wellcome_2, ""))
        }

        mBannerAdapter = ImageBannerAdapter(bannerData)

        mDataBinding.appBanner.run {
            setAdapter(mBannerAdapter)
            //关联Banner与Activity的生命周期
            addBannerLifecycleObserver(this@SplashActivity)
            //设置指示器的样式
            indicator = CircleIndicator(this@SplashActivity)
            //设置切换效果
            addPageTransformer(AlphaPageTransformer())
            //设置页面切换监听事件
            addOnPageChangeListener(object : OnPageChangeListener{
                override fun onPageScrolled(position: Int,positionOffset: Float, positionOffsetPixels: Int) { }

                override fun onPageSelected(position: Int) {
                    //滑动到最后一页
                    if (position == bannerData.size - 1){
                        mDataBinding.tvStart.visibility = View.VISIBLE
                    }else{
                        mDataBinding.tvStart.visibility = View.INVISIBLE
                    }
                }

                override fun onPageScrollStateChanged(state: Int) { }

            })
        }

        //设置立即进入的点击事件
        mDataBinding.tvStart.setOnClickListener {
            this@SplashActivity.jumpToMainPage()
        }

    }

    private fun showAppAdvLayout(){
        mDataBinding.clAppAdv.visibility = View.VISIBLE
        mDataBinding.clAppBanner.visibility = View.INVISIBLE

        //5秒倒计时
        mCountDownTimer = object : CountDownTimer(5000, 1000){
            override fun onTick(millisUntilFinished: Long) {
                mDataBinding.tvAdvSkip.text = "${millisUntilFinished / 1000}秒跳过"
            }

            override fun onFinish() {
               this@SplashActivity.jumpToMainPage()
            }
        }

        //启动倒计时
        mCountDownTimer.start()
    }


    private fun jumpToMainPage(){
        ARouter.getInstance().build(RoutePath.PATH_MAIN_PAGE).navigation()
//        ARouter.getInstance().build(RoutePath.PATH_MAIN_PAGE).navigation(this@SplashActivity, LocalDegradeService())
        this@SplashActivity.finish()
    }


}